import type { components } from "@/genapi/wh";
import { whManagerBsTbEvaluateReportGetPageUsingPost } from "@/genapi/wh";
import { usePage } from "@/hooks/use-page";
import { useCompanyStore } from "@/stores/company";

import type DetailDialog from "./components/detail-dialog.vue";

export const [createState, injectState] = createInjectionState(() => {
	const companyStore = useCompanyStore();

	const filter = ref<components["schemas"]["TbEvaluateReportQueryDTO"]>();

	const page = usePage({
		initPageSize: 15,
		fetch: async ({ pageSize, currentPage }) => {
			const { data } = await whManagerBsTbEvaluateReportGetPageUsingPost({
				data: {
					size: pageSize,
					current: currentPage,
					query: {
						...filter.value,
						enterpriseId: companyStore.currentCompanyId,
					},
				},
			});
			return {
				records: data?.records ?? [],
				total: data?.total ?? 0,
				pageSize,
			};
		},
	});

	watchDeep(filter, () => page.execute({ currentPage: 0 }));

	// 当前选中的列表项
	const selectedList = shallowRef<
		components["schemas"]["TbEvaluateReportDuiXiang"][]
	>([]);

	// 表格组件
	const tableRef = shallowRef<InstanceType<GlobalComponents["TableWidget"]>>();

	// 详情弹窗组件
	const detailDialogRef = shallowRef<InstanceType<typeof DetailDialog>>();

	return {
		page,
		filter,
		selectedList,
	};
});
